﻿using MovingChame.Web.Infrastructure;
using MovingChame.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;

namespace MovingChame.Web.Services
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class FriendService : IFriendService
    {
        IFriendProvider friendProvider;

        public FriendService() 
        {
            friendProvider = FriendProvider.Current;
        }

        public List<FriendData> GetFriends(int userId)
        {
            List<Friend> friends = friendProvider.GetFriends(userId);
            List<FriendData> result = new List<FriendData>();

            foreach (Friend friend in friends)
            {
                FriendData friendData = new FriendData() {
                    Id = friend.Id,
                    Status = friend.FriendStatus,
                    Email = friend.User1.Email,
                    Username = friend.User1.Username
                };
                result.Add(friendData);
            }
            return result;
        }

        public List<FriendData> GetRequestedFriends(int userId)
        {
            List<Friend> friends = friendProvider.GetRequestedFriends(userId);
            List<FriendData> result = new List<FriendData>();

            foreach (Friend friend in friends)
            {
                FriendData friendData = new FriendData()
                {
                    Id = friend.Id,
                    Status = friend.FriendStatus,
                    Email = friend.User.Email,
                    Username = friend.User.Username
                };
                result.Add(friendData);
            }
            return result;
        }

        public List<FriendData> GetRequestingFriends(int userId)
        {
            List<Friend> friends = friendProvider.GetRequestingFriends(userId);
            List<FriendData> result = new List<FriendData>();

            foreach (Friend friend in friends)
            {
                FriendData friendData = new FriendData()
                {
                    Id = friend.Id,
                    Status = friend.FriendStatus,
                    Email = friend.User1.Email,
                    Username = friend.User1.Username
                };
                result.Add(friendData);
            }
            return result;
        }

        public int AddFriend(int userId, int userId1)
        {
            return friendProvider.AddFriend(userId, userId1);
        }

        public bool Unfriend(int userId, int userId1)
        {
            return friendProvider.Unfriend(userId, userId1);
        }

        public bool CancleFriendRequest(int userId, int userId1)
        {
            return friendProvider.CancleFriend(userId1, userId1);
        }
    }
}
